
$(function () {
    getTree();
    $("#jqGrid").jqGrid({
        url: '../sysarea/list',
        datatype: "json",
        colModel: [			
			{ label: 'areaId', name: 'areaId', index: 'area_id', width: 50, key: true,hidden:true },
			{ label: '父级编号', name: 'parentId', index: 'parent_id', width: 80 ,hidden:true},
            { label: '父级名称', name: 'parentName', index: 'parent_name', width: 80 },
            { label: '名称', name: 'name', index: 'name', width: 80 },
			{ label: '排序', name: 'sort', index: 'sort', width: 80,hidden:true },
			{ label: '区域编码', name: 'code', index: 'code', width: 80 ,hidden:true},
			{ label: '区域类型', name: 'type', index: 'type', width: 80,hidden:true },
			{ label: '级别', name: 'level', index: 'level', width: 80 ,hidden:true},
			{ label: '备注信息', name: 'remarks', index: 'remarks', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        parentId:"",
		sysArea: {},
		q:{
            parentId:null,
            parentName:null,
            level:null,
            parentNameup:null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){

			if(vm.q.parentId==null||vm.q.parentId==undefined||vm.q.parentId==""){
                alert("请选择父级区域！");
                return;
			}
			vm.showList = false;
			vm.title = "新增";
			vm.sysArea = {
                parentId:vm.q.parentId,
         	   parentName:vm.q.parentName,
                level:vm.q.level
			};
		},
		update: function (event) {
            if(vm.q.parentId==null||vm.q.parentId==undefined||vm.q.parentId==""){
                alert("请选择修改区域！");
                return;
            }
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(vm.q.parentId)
		},
		saveOrUpdate: function (event) {
			var url = vm.sysArea.areaId == null ? "../sysarea/save" : "../sysarea/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.sysArea),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
            if(vm.q.parentId==null||vm.q.parentId==undefined||vm.q.parentId==""){
                alert("请选择父级区域！");
                return;
            }
            if(len!=0){
                alert("请选最下级节点再进行删除操作！");
                return;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../sysarea/delete",
				    contentType: "application/json",
				    data: JSON.stringify([vm.q.parentId]),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(areaId){
			$.get("../sysarea/info/"+areaId, function(r){
                vm.sysArea = r.sysArea;
                vm.sysArea.parentName = vm.q.parentNameup;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'parentId': null},
                page:page
            }).trigger("reloadGrid");
            getTree();
		},
        getSingleRow: function(){
            var grid = $("#jqGrid");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择一条记录");
                return ;
            }
            return rowKey;
        },
         clickmp:function(){
             getTree();
        },
        areamove:function () {
            if(vm.q.parentId==null||vm.q.parentId==undefined||vm.q.parentId==""){
                alert("请选择迁移区域！");
                return;
            }
            openDialogTreeView('选择区域',$("#windowtree"),'400px','550px',1);
        }
       
	}
});
var ztree = null;
var dataTree = null;
function getTree(){
    vm.q.parentId=null;
    //加载菜单树
    $.get("../sysarea/tree", function(r){
        ztree = $.fn.zTree.init($("#menuTree"), setting, r.list);
        //展开所有节点
        ztree.expandAll(true);
        dataTree = $.fn.zTree.init($("#menuTreeselect"), settingselect, r.list);
        //展开所有节点
        dataTree.expandAll(true);
		/*if(roleId != null){
		 vm.getRole(roleId);
		 }*/
    });
}
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: "id"
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:false,
        nocheckInherit:true
    },
    callback:{
        //beforeClick: getCurrentNode
        onClick : zTreeOnClick
    }
};
var settingselect = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: "id"
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:false,
        nocheckInherit:true
    },
    callback:{
        //beforeClick: getCurrentNode
        onClick : zTreeOnClickselect
    }
};

var len=0;
function zTreeOnClick(event, treeId, treeNode){
	vm.q.treeNode=treeNode;
    vm.q.parentName=treeNode.name;
    len=treeNode.children.length;
    vm.q.parentId = treeNode.areaId;
    vm.q.level = treeNode.level;
    if(treeNode.areaId!='1'){
        vm.q.parentNameup = treeNode.getParentNode().name;
	}
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        postData:{'parentId': vm.q.parentId},
        page:page
    }).trigger("reloadGrid");

};
var parentareaId=null;
function zTreeOnClickselect(event, treeId, treeNode){
    parentareaId = treeNode.areaId;
};
//打开一颗树
openDialogTreeView=function(title,url,width,height,type){
    if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
        width='auto';
        height='auto';
    }else{//如果是PC端，根据用户设置的width和height显示。

    }
    layer.open({
        type: type,
        offset: '50px',
        skin: 'layui-layer-molv',
        title: title,
        area: [width, height],
        maxmin: true, //开启最大化最小化按钮
        content: url,
        btn: ['确定','关闭'],
        btn1:function(index, layero){
            if(parentareaId==null||parentareaId==undefined||parentareaId==""){
                alert("请选需要迁移到哪个区域！");
                return;
            }
            if(parentareaId==vm.q.parentId){
                alert("迁移区域相同！");
                return;
			}
            $.ajax({
                type: "POST",
                url: "../sysarea/updatearea?parentareaId="+parentareaId+"&&areaId="+vm.q.parentId,
                contentType: "application/json",
				dataType:'json',
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();

                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
            layer.close(index);
        },
        btn2:function(index){
            layer.close(index);
        }
    });
}
